﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VideoRentalShop.Controller;
using VideoRentalShop.Model;
using VideoRentalShop.View.Templates;
using System.Windows;
using System.Windows.Data;

namespace VideoRentalShop.View.Windows.OrderList
{
    public class OrderListPanel : ListPanel
    {
        private VideoRentalShopController _controller = VideoRentalShopController.Instance;
        private SearchPanel _panSearch;
        private AdditionalPanel _panDetails;


        public OrderListPanel()
        {
            _panSearch = new SearchPanel();
            _panDetails = new AdditionalPanel();
            AddSearchPanel(_panSearch);
            AddAdditionalPanel(_panDetails, new GridLength(1d, GridUnitType.Star));
            CreateListColumns();
        }


        protected override void Refresh()
        {
            _panSearch.Reload();
            _panSearch.Clear();
            _panDetails.Clear();
        }

        private void CreateListColumns()
        {
            AddListColumn("Data utowrzenia", new Binding { Path = new PropertyPath("Date"), StringFormat = "{0:dd.MM.yyyy HH:mm}" });
            var binding = new MultiBinding { StringFormat = "{0} {1}" };
            binding.Bindings.Add(new Binding("ClientLastName"));
            binding.Bindings.Add(new Binding("ClientName"));
            AddListColumn("Klient", binding);
            AddListColumn("Zakończone", "Archived");
        }
        
        protected override void ClearClicked()
        {
            _panSearch.Clear();
        }

        protected override void SearchClicked()
        {
            var results = _panSearch.GetResults();
            ShowResults(results);
        }
        
        private void ShowResults(IEnumerable<Order> results)
        {
            var list = new List<dynamic>();

            foreach (var result in results.OrderBy(x => x.MakeDate))
            {
                list.Add(new
                {
                    Item = result,
                    Date = result.MakeDate,
                    ClientLastName = result.Client.LastName,
                    ClientName = result.Client.Name,
                    Archived = result.Archived ? "Tak" : "Nie"
                });
            }

            ShowListResults(list);
        }


        protected override void RemoveClicked()
        {
            var count = lstResults.SelectedItems.Count;
            var completed = 0;

            foreach (dynamic lstItem in lstResults.SelectedItems)
            {
                var item = (Order)lstItem.Item;

                if (_controller.Data.Order.Remove(item))
                {
                    ++completed;
                }
            }

            if (completed != count)
            {
                _controller.ShowMessage("Podczas usuwania zamówień wystąpił błąd", "Błąd");
            }
            
            if (completed > 0)
            {
                RefreshList();
            }
        }
        
        private void RefreshList()
        {
            SearchClicked();
        }
        
        protected override void SelectionChanged()
        {
            var itemsCount = lstResults.SelectedItems.Count;

            if (itemsCount > 0)
            {
                dynamic lstItem = lstResults.SelectedItem;
                var item = lstItem.Item as Order;

                _panDetails.LoadItem(item);
            }
            else
            {
                _panDetails.LoadItem(null);
            }
        }
    }
}
